<?php
/**
 * Représente un mot en Japonais
 *
 * @author Kévin Fauconnet (05/04/2013)
 */
 
class Term extends SimpleModel {
	
	public $id_term;
	public $kana;
	public $tone;
	public $pronunciation_romaji;
	public $kanji;
	
	public $last_update;
	
	public function sqltable() { return "terms"; }
	public function schema() { return array(
		'id_term' 				=> array('primarykey' => true, 'type' => 'int'),
		'kana' 					=> 'string(255)',
		'tone'					=> 'string(255)',
		'pronunciation_romaji' 	=> 'string(255)',
		'kanji'					=> 'string(255)',
		'last_update'			=> 'datetime'
	);}
	
	/**
	 * Crée un nouvel objet
	 */	
	public function __construct($param = 0) {
		$this->id_term = 0;
		$this->kana = "";
		$this->tone = "";
		$this->pronunciation_romaji = "";
		$this->kanji = "";
		
		$this->last_update = null;

		$this->init($param);

	}
	
	/**
	 * Sauvegarde l'objet, et met à jour l'attribut last_update
	 */	
	public function save($options = array()) {
		$this->last_update = date('d/m/Y H:i:s');

		return parent::save($options);
	}
	
	public static function getTranslationsTerm($id_term) {
		require_once('translation.class.php');
		return Translation::getTranslations(array(
			'id_term' => $id_term
		));
	}
	
	/**
	 * Renvoie les traductions lié à un terme
	 */
	public function getTranslations() {
		return self::getTranslationsTerm($this->id_term);
	}
	
	/**
	 * Crée la requête de listing des termes et l'exécute
	 */
	public function buildQueryTerms($options = array()) {
		$sql = "SELECT * FROM terms";
		$where = array();
		
		if (!empty($options['count'])) {
			$sql = "SELECT COUNT(*) FROM terms";
		}
		
		if (!empty($options['tone'])) {
			$where[] = array('AND', 'tone = ?', $options['tone']);
		}
		if (!empty($options['pronunciation_romaji'])) {
			$where[] = array('AND', 'pronunciation_romaji = ?', $options['pronunciation_romaji']);
		}
		if (!empty($options['kana'])) {
			$where[] = array('AND', 'BINARY kana LIKE ?', $options['kana']);			
		}
		
		$queryOptions = array(
			'query' => $sql,
			'where' => $where
		);
		
		if (empty($options['count'])) {
			$queryOptions += array(
				'orderby' => 	self::getOption($options, 'orderby', 'kana'),
				'limit' => 		self::getOption($options, 'limit'),
				'offset' => 	self::getOption($options, 'offset'),
				'page' => 		self::getOption($options, 'page')
			);
		}
		
		return self::buildQuery($queryOptions);
	}
	
	/**
	 * Renvoie le total de termes
	 */
	public function countTerms($options = array()) {
		$options['count'] = true;
		
		$st = self::buildQueryTerms($options);
		$total = $st->fetchOne('int');
		return $total;
	}
	
	/**
	 * Renvoie une liste de termes
	 */
	public function getTerms($options = array()) {
		$st = self::buildQueryTerms($options);
		
		$results = array();
		
		while ($st->fetch()) {
			$results[] = new self($st);
		}
		return $results;
	}
	
	public function getTerm($options = array()) {
		$st = self::buildQueryTerms($options);
		
		$o = null;
		if ($st->fetch()) {
			$o = new self($st);
		}
		return $o;
	}
	
	public function getToneHTML() {
		$kana = $this->kana;
		$html = "";
		
		for ($i = 0; $i < mb_strlen($kana,'UTF-8'); $i++) {
			$kanal = mb_substr($kana,$i,1,'UTF-8');
			$class = "tone";
			if (isset($this->tone[$i])) {
				switch($this->tone[$i]) {
					case 'u':
						$class .= " up";
						break;
					case 'd':
						$class .= " down";
						break;
				}
			}
			$html .= '<span class="'.$class.'">'.$kanal.'</span>';
		}
		
		return $html;
	}
	
	public function isToneDistinct() {
		$tone = trim($this->tone);
		$distinct = !empty($tone);
		return  $distinct;
	}
	
}
?>